package com.whfc.wxmp.api.fse.controller;

import com.whfc.common.result.ListData;
import com.whfc.common.result.R;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.wxmp.annotation.CurrentMsUser;
import com.whfc.wxmp.annotation.CurrentWxUser;
import com.whfc.fse.dto.FseCrawlerDTO;
import com.whfc.fse.dto.FseCrawlerDataDTO;
import com.whfc.fse.dto.FseCrawlerDataLogDTO;
import com.whfc.fse.dto.FseFvsDeviceDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.entity.WxUser;
import com.whfc.wxmp.api.fse.service.WxFseCrawlerService;
import com.whfc.wxmp.util.RequestConetxtUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author qzexing
 * @version 1.0
 * @date 2021/11/4 16:32
 */
@RestController
@RequestMapping("/mp/api/fse/crawler")
public class WxFseCrawlerController {


    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxFseCrawlerService wxFseCrawlerService;

    @GetMapping("/list")
    public R<List<FseCrawlerDTO>> list(@CurrentWxUser WxUser loginUser,
                  @RequestParam("deptId") Integer deptId,
                  @RequestParam(value = "bindFlag", required = false) Integer bindFlag,
                  @RequestParam(value = "netState", required = false) Integer netState) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                loginUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "履带吊列表", RequestConetxtUtil.getParameters());
        List<FseCrawlerDTO> listData = wxFseCrawlerService.list(deptId, bindFlag, netState);
        return R.success(listData);
    }

    @GetMapping("/latest")
    public Result latest(@CurrentWxUser WxUser loginUser,
                         @RequestParam("crawlerId") Integer crawlerId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                loginUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "履带吊最新数据", RequestConetxtUtil.getParameters());
        FseCrawlerDataDTO crawlerDataDTO = wxFseCrawlerService.getLatest(crawlerId);
        return ResultUtil.success(crawlerDataDTO);
    }

    @GetMapping("/log")
    public Result log(@CurrentWxUser WxUser loginUser,
                      @RequestParam("crawlerId") Integer crawlerId,
                      @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                loginUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "履带吊历史数据", RequestConetxtUtil.getParameters());
        List<FseCrawlerDataLogDTO> list = wxFseCrawlerService.getCrawlerLog(crawlerId, date);
        return ResultUtil.success(new ListData<>(list));
    }


    @GetMapping("/log/v2")
    public Result log(@CurrentMsUser SysUser user,
                      @RequestParam("crawlerId") Integer crawlerId,
                      @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
                      @RequestParam(value = "field", required = false) String field) {
        logger.info("履带吊历史数据，loginer:{},crawlerId:{}, time:{},field:{}", user.getUsername(), crawlerId, date, field);
        List<FseCrawlerDataLogDTO> list = wxFseCrawlerService.getCrawlerLog(crawlerId, date, field);
        return ResultUtil.success(new ListData<>(list));
    }


    @GetMapping("/fvs/list")
    public Result fvsList(@CurrentWxUser WxUser user,
                          @RequestParam("crawlerId") Integer crawlerId) {
        logger.info("获取视频监控列表，loginer:{},crawlerId:{}", user.getName(), crawlerId);
        List<FseFvsDeviceDTO> fvsList = wxFseCrawlerService.fvsList(crawlerId);
        return ResultUtil.success(new ListData<>(fvsList));
    }
}
